<?php
namespace app\admin\model;
class Webuploader
{
    public function Webuploader($temdir=null){
        $data = request()->param();
        $name = explode('.',$data['name']);
        if(!$temdir){
            if(session('modular')){
                $temdir = './uploads/'.session('modular').'/temp/'.$name[0];
            }else{
                $temdir = './uploads/'.request()->module().'/temp/'.$name[0];
            }
        }
        $file = request()->file('file');
        $fileinfo = $file->getInfo();
        $number = $data['chunk']??0;
        if(!is_dir($temdir)){
            mkdir($temdir,0777,true);
        }
        move_uploaded_file($fileinfo['tmp_name'],$temdir.'/'.$name[0].'_'.$number.'.part');
        return $data;
    }
    public function hebing($temdir=null,$hebin=null){
        if(!$hebin){
            if(session('modular')){
                $hebin =  './uploads/'.session('modular').'/'.date('Ymd',time());
            }else{
                $hebin = './uploads/'.request()->module().'/'.date('Ymd');
            }
        }
        if(!is_dir($hebin)){
            mkdir($hebin,0777,true);
        }
        $data = request()->param();
        $name = explode('.',$data['name']);
        if(!(isset($data['new_name'])&&$data['new_name'])){
            $data['new_name'] = uniqid().'.'.end($name);
        }
        if(!$temdir){
            if(session('modular')){
                $temdir = './uploads/'.session('modular').'/temp/'.$name[0];
            }else{
                $temdir = './uploads/'.request()->module().'/temp/'.$name[0];
            }
        }
        $data['sucees'] = false;
        $start = $data['satrt']??0;
        for ($a=0;$a<10;$a++){
            if(is_file($temdir.'/'.$name[0].'_'.$start.'.part')){
                $str = file_get_contents($temdir.'/'.$name[0].'_'.$start.'.part');
                file_put_contents($hebin.'/'.$data['new_name'],$str,FILE_APPEND);
                unlink($temdir.'/'.$name[0].'_'.$start.'.part');
            }else{
                $data['sucees'] = true;
                rmdir($temdir);
                break;
            }
            $start++;
        }
        $data['satrt'] = $start;
        return $data;
    }
}